package com.example.dingding.controller;

import com.example.dingding.config.HttpHelper;
import com.example.dingding.entity.UseCar;
import com.example.dingding.entity.dto.UseCarDto;
import com.example.dingding.entity.dto.UseCarSmsDTO;
import com.example.dingding.mapper.UseCarMapper;
import com.example.dingding.service.SmsService;
import com.example.dingding.service.UseCarService;
import com.example.dingding.util.ResponseResult;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//用车审批
@RestController
public class UseCarController {

    @Autowired
    private HttpHelper httpHelper;

    @Autowired
    private UseCarService useCarService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private UseCarMapper useCarMapper;

    //    @Value("${azure.corpid}")
    @Value("ddingbeeebed0cb750c8135c2f4657eb6378f")
    private String coprid;

    //    @Value("${azure.appkey}")
    @Value("dingzvtl6gimsru7ymlx")
    private String appkey;

    //    @Value("${azure.appsecret}")
    @Value("Fj4xtc4l5cScmZhfNCkAcCMPFRA5XKq26BxDYOWCMQrra6iCSbmGuKIYkPrx9t9o")
    private String appsecret;

    //    @Value("${azure.agentid}")
    @Value("3174139988")
    private String agentid;

    //oa审批提交
    @RequestMapping("/UseCar")
    public ResponseResult add(@RequestBody UseCarDto useCarDto) {
//        , String requestAuthCode
        //获取数据
        UseCar useCar = new UseCar();
        useCar.setId(useCarDto.getId());
        useCar.setDegreeUrgency(useCarDto.getDegreeUrgency());
        //获取钉钉userid，详情参考dingding后台管理用户管理界面,
        List<String> userids = useCarDto.getUserid();
        //获取乘车人
        List<String> rider = useCarDto.getRider();
        //乘车人数
        int riderNumber = useCarDto.getRiderNumber();
        //乘车人电话
        String initiatorPhone=useCarDto.getInitiatorPhone();
        List<String> phone = useCarDto.getPhone();
        useCar.setRider(String.valueOf(rider));
        useCar.setPhone(String.valueOf(phone));
        useCar.setInitiatorPhone(String.valueOf(initiatorPhone));
        useCar.setVehicleCategory(useCarDto.getVehicleCategory());
        useCar.setDestinationArea(useCarDto.getDestinationArea());
        useCar.setSpecificDestination(useCarDto.getSpecificDestination());
        useCar.setCarDriven(useCarDto.getCarDriven());
        useCar.setUseTime(useCarDto.getUseTime());
        useCar.setVehicleType(useCarDto.getVehicleType());
        useCar.setReturnTime(useCarDto.getReturnTime());
        useCar.setWorkingDay(useCarDto.getWorkingDay());
        useCar.setDepartment(useCarDto.getDepartment());
        useCar.setStartingPoint(useCarDto.getStartingPoint());
        useCar.setExpenses(useCarDto.getExpenses());
        useCar.setRiderNumber(String.valueOf(riderNumber));
        //获取审批人
        List<String> divisionLeader = useCarDto.getDivisionLeader();
        List<String> logistiServiceLeadership = useCarDto.getLogistiServiceLeadership();
        List<String> logisticslineManager = useCarDto.getLogisticslineManager();
        List<String> logisticsFleet = useCarDto.getLogisticsFleet();

//抄送人
//        List<String> copyUser = useCarDto.getCopyUser();
        //获取审批实例id
        String instanceId = useCarService.saveApproval(useCar,useCarDto.getRequestAuthCode(), divisionLeader, logistiServiceLeadership, logisticslineManager, logisticsFleet);
        useCar.setInstanceId(instanceId);
        //审批类型1
        useCar.setApprovalCount(0);
        useCarService.save(useCar);


        /**为方便测试，将SmsService中的方法存放此处。*/
        //获取司机信息和车牌号、行车公里数
//        UseCar message = smsService.carMessage(instanceId);
//        System.out.println(message);
//        //司机电话
//        String carMessage = message.getCarMessage();
//        //判断车管是否修改
//        carMessage  = carMessage .substring(1, carMessage .length() - 1);
//        if(instanceId!=null&&!carMessage.equals("由车管填写")) {
//            //车牌
//            String carNumber = message.getCarMessage();
//            //用车时间
//            String useTime = message.getUseTime();
//            //具体目的地
//            String specificDestination = message.getSpecificDestination();
//            //出发地点
//            String startingPoint = message.getStartingPoint();
//            //获取流水号
//            String serialNumber = smsService.sSerialNumber();
//            //短信内容拼接
//            UseCarSmsDTO car = new UseCarSmsDTO();
//            //企业编号
//            car.setSpCode("263092");
//            //用户名
//            car.setLoginName("cs_rmyh");
//            //密钥
//            car.setPassword("e233ee84cf769cffee50f55352ca88fa");
//            //短信内容
//            //模板内容:
//            //您明天驾驶(xxxxxxxx}的车辆于{xxxxxxxxxxxxxxxx}
//            // 前往{xxxxxxxxxxxxxxxxxxxxxHx}请提前到达{xxxxxxxxxxxxxxxxxxxxx}等候
//            car.setMessageContent("您明天驾驶" + carNumber.replace("\"", "") + "的车辆于" + useTime.replace("\"", "") + "前往" + specificDestination.replace("\"", "") + "请提前到达" + startingPoint.replace("\"", "") + "等候。");
//            //电话号码
//            car.setUserNumber(carMessage);
//            //短信模块id
//            car.setTemplateId("2151012276881");
//            //流水号
//            car.setSerialNumber(serialNumber);
//            car.setF("f");
//
////                smsService.sendMessage(car);
//
//            //短信通知乘车人
//            //获取乘车人员信息
//            String riderPeople = useCarMapper.selectProcessInstanceid(instanceId);
//            //// 使用正则表达式匹配括号内的所有数字
//            Pattern pattern = Pattern.compile("\\d+");
//            Matcher matcher = pattern.matcher(riderPeople);
//            List<String> numberList = new ArrayList<>();
//
//            while (matcher.find()) {
//                // 将匹配到的数字添加到List集合中
//                numberList.add(matcher.group());
//            }
//
//            //乘车人员
//            UseCarSmsDTO riderHuman = new UseCarSmsDTO();
//            //企业编号
//            riderHuman.setSpCode("263092");
//            //用户名
//            riderHuman.setLoginName("cs_rmyh");
//            //密钥
//            riderHuman.setPassword("e233ee84cf769cffee50f55352ca88fa");
//            // 您明天前往{xxxxxxxxxxxxxxxxxxxxx}的车辆号牌为{x}{xxxxxxxxxxx}
//            // {x}司机号码{x}{xxxxxxxxxxxxx}{x}{xxxxxxxxxxxxxxxxxxxx}在
//            // {xxxxxxxxxxxxxxxxxxxxxxx}出发{x}敬请知悉{x}
//            riderHuman.setMessageContent("您明天前往" + specificDestination.replace("\"", "") + "的车辆号牌为:" + carNumber.replace("\"", "") + ",司机号码:" + carNumber.replace("\"", "") + "," + useTime.replace("\"", "") + "在" + startingPoint.replace("\"", "") + "出发,敬请知悉。");
//            //短信模块id
//            riderHuman.setTemplateId("2151012276862");
//            //流水号
//            riderHuman.setSerialNumber(serialNumber);
//            riderHuman.setF("f");
//            if(numberList!=null){
//                for (String s : numberList) {
//                    //电话号码
//                    riderHuman.setUserNumber(s);
//
////                    smsService.sendMessage(riderHuman);
//                }
//            }
//        }else{
//            System.out.println("您未修改信息");
//        }


        return ResponseResult.okResult();
    }

//    //获取钉钉jsapi_ticket
//    @GetMapping("/accessToken")
//    public String setToken() throws Exception {
//        String jsapiTickets = httpHelper.getJsapiTicket();
//        return jsapiTickets;
//    }

    //获取当前发起人电话号码
    @GetMapping("/getPhone")
    public List<String> getPhone(@RequestParam(name = "listUserid", required = false) List<String> listUserid) {

        return useCarService.getPhone(listUserid);
    }

    //获取所有角色信息
    @GetMapping("/getRole")
    public List<String> getRoleLogin() {
        return useCarService.getRole();
    }

    //获取乘车人信息
    @GetMapping("/getUserPhone")
    public String getUserPhone(String requestAuthCode) {
        return useCarService.getUserPhone(requestAuthCode);
    }


//        @RequestMapping("/carRentral")
//    public ResponseResult rentral(@RequestBody UseCarDto useCarDto) {
//            //获取数据
//
//
//    return ResponseResult.okResult();
//    }
}














